/*************************************************************************
                           Destockage.c  -  description
                             -------------------
    début                : 15 Novembre 2012
*************************************************************************/

//---------- Réalisation du thread Destockage (fichier Destockage.c) -----

/////////////////////////////////////////////////////////////////  INCLUDE
//-------------------------------------------------------- Include système


#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <mqueue.h>
#include <sys/stat.h>
#include <errno.h>
#include <time.h>
//------------------------------------------------------ Include personnel
#include "../Outils/config.h"
#include "../Outils/simulation.h"
#include "destockage.h"


void * Destockage()
{

	//-------------------------------Phase initialisation--------------------------
	int bytes_read;
	unsigned int msgPrio = PRIO_MSG_L;

	//Declaration des BAL
	mqd_t queue_versDestockage;
	mqd_t queue_versArchivage;

	//Declaration des messages
	MSG_versArchivage msgDestockage_archivage;
	MSG_versDestockage msgDestockage;


	//ouverture de BAL_versStockage en lecture seule
	queue_versDestockage =  mq_open(BAL_versDestockage, O_RDONLY);

	//ouverture de BAL_versArchivage en écriture seule
	queue_versArchivage =  mq_open(BAL_versArchivage, O_WRONLY);


	//-------------------------------Phase moteur--------------------------
	for ( ; ;)
	{
		  //attente d'un message de Windows
		  bytes_read = mq_receive(queue_versDestockage, (char*)&msgDestockage, sizeof(MSG_versDestockage), NULL);

		  if (bytes_read == -1)
		  {
		   	perror("[Destockage] Failed to recieve BAL_versDestockage:");
		  }
		  else
		  {
			//lecture des messages
			int nbPalA=msgDestockage.nbPaletteA;
			int nbPalB=msgDestockage.nbPaletteB;
	
			if (nbPalA == -1)
			{
				//Fermeture des BAL
				mq_close (queue_versArchivage); 
				mq_close (queue_versDestockage); 
				pthread_exit(0);
			}
			else
			{

				//Decrementation du nombre de palettes A dans la mempartagee
				pthread_mutex_lock(MutexSHMStock);
				ZoneStockage->nbPaletteA-=nbPalA;
				pthread_mutex_unlock(MutexSHMStock);

				//Decrementation du nombre de palettes B dans la mempartagee
				pthread_mutex_lock(MutexSHMStock);
				ZoneStockage->nbPaletteB-=nbPalB;
				pthread_mutex_unlock(MutexSHMStock);

				//ecriture du message msgPalette_archivage
				msgDestockage_archivage.source=T_DESTOCKAGE;
				msgDestockage_archivage.message=DESTOCK_OK;
				msgDestockage_archivage.heure=time(NULL);
				msgDestockage_archivage.numLot=0;
				msgDestockage_archivage.cartonEnCours=0;
				msgDestockage_archivage.type=ARCHIVAGE;

				//envoi du message msgPaletteOK à Archivage
				if (0 != mq_send(queue_versArchivage,(const char*)&msgDestockage_archivage,sizeof(MSG_versArchivage),msgPrio))
				{
					perror ("[Destockage] Failed to Send BAL_versArchivage:");
				}
				#ifdef DEBUG
				printf("[DESTOCKAGE] Palette(s) destockees \n");
				#endif
			}
	

		  } //fin if reception

	}
	//-------------------------------FIN Phase moteur--------------------------


} //fin procedure

